package com.dinglian.ots.controller.common;

import com.dinglian.ots.common.model.Constant;
import com.dinglian.ots.dao.model.Country;
import com.dinglian.ots.dao.model.District;
import com.dinglian.ots.dao.model.Province;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.CommonService;
import com.dinglian.ots.vo.address.DistrictVo;
import com.dinglian.ots.vo.address.ProvinceVo;
import com.dinglian.ots.vo.buyer.MessageVo;
import com.dinglian.ots.vo.product.*;
import com.dinglian.ots.vo.user.LoginByPasswordVo;
import com.dinglian.ots.vo.user.UserVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Validated
@Api(value = "/common", tags = "通用模块")
@RequestMapping("/common/")
@CrossOrigin
public class CommonController {

    @Autowired
    private CommonService commonService;

    @ApiOperation("发送验证码")
    @GetMapping("sendCode")
    public Result sendCode(@RequestParam(value = "mobile", required = true) String mobile) {
        String code = "";
        try {
            code = commonService.sendCode(mobile);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(code);
    }

    @ApiOperation("验证手机验证码")
    @GetMapping("checkCode")
    public Result checkCode(@RequestParam(value = "mobile") @Pattern(regexp = "^1\\d{10}$", message = "手机号码格式错误") String mobile,
                            @RequestParam(value = "code") String code) {
        try {
            commonService.checkCode(mobile, code);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @ApiOperation("获取所有一级品类")
    @GetMapping("getCategoryList")
    public Result getCategoryList() {
        List<CategoryVo> categoryVo = null;
        try {
            categoryVo = commonService.getCategoryList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(categoryVo);
    }

    @ApiOperation("获取所有国家")
    @GetMapping("getCountry")
    public Result getCountry() {
        List<Country> countries = null;
        try {
            countries = commonService.getCountry();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(countries);
    }



    @ApiOperation("获取省份城市")
    @GetMapping("getProvinceCity")
    public Result getProvinceCity() {
        List<ProvinceVo> provinceVos = null;
        try {
            provinceVos = commonService.getProvinceCity();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(provinceVos);
    }


    @ApiOperation("获取子节点目的地")
    @GetMapping("getChildDistrict")
    public Result getChildDistrict(@RequestParam("parentId")Integer parentId) {
        List<District> districtVos = null;
        try {
            districtVos = commonService.getChildDistrict(parentId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(districtVos);
    }


    @ApiOperation("获取所有商品品类")
    @GetMapping("getSubcategoryList")
    public Result getSubcategoryList() {
        List<ProductCategoryVo> categoryVo = null;
        try {
            categoryVo = commonService.getSubcategoryList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(categoryVo);
    }

    @ApiOperation("获取所有风格")
    @GetMapping("getStyleList")
    public Result getStyleList() {
        List<StyleVo> styleVo = null;
        try {
            styleVo = commonService.getStyleList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(styleVo);
    }

    @ApiOperation("获取所有适用人群")
    @GetMapping("getCrowdList")
    public Result getCrowdList() {
        List<CrowdVo> crowdVos = null;
        try {
            crowdVos = commonService.getCrowdList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(crowdVos);
    }

    @ApiOperation("获取所有季节")
    @GetMapping("getSeasonList")
    public Result getSeasonList() {
        List<SeasonVo> seasonList = null;
        try {
            seasonList = commonService.getSeasonList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(seasonList);
    }

    @ApiOperation("获取所有颜色")
    @GetMapping("getColorList")
    public Result getColorList() {
        List<ColorVo> colorList = null;
        try {
            colorList = commonService.getColorList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(colorList);
    }

    @ApiOperation("获取所有尺寸")
    @GetMapping("getSizeList")
    public Result getSizeList() {
        List<SizeVo> sizeList = null;
        try {
            sizeList = commonService.getSizeList();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(sizeList);
    }

    @ApiOperation(value = "查看站内信")
    @GetMapping("getMessageList")
    public Result getMessageList(@RequestParam("userId") Integer userId,
                                 @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        PageInfo<MessageVo> messageList = new PageInfo<>();
        try {
           /* UserVo userVo = (UserVo) SecurityUtils.getSubject().getSession().getAttribute(Constant.SESSION_USER);
            if (!userVo.getUserId().equals(userId)){
                throw new GlobalException(CodeMsg.MESSAGE_OWN);
            }*/
            messageList = commonService.getMessageList(userId,pageNum,pageSize);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(messageList);
    }

    @ApiOperation(value = "删除站内信")
    @GetMapping("deleteMessage/{messageId}")
    public Result deleteMessage(@PathVariable("messageId") Integer messageId) {
        try {
            commonService.deleteMessage(messageId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation(value = "用于富文本上传图片")
    @PostMapping("uploadPicture")
    public String uploadPicture(MultipartFile file) {
        return commonService.uploadPicture(file);
    }

}
